import hashlib
import os

class Repo:
    def __init__(self):
        self.hashid     = None
        self.path       = None
        self.alias      = []
        self.workingdir = []

    def __cmp__(self, y):
        return self.hashid == y.hashid
    
        

class Keeper:
    def __init__(self, repo_path):            
        self.__map_hashid     = {}
        self.__map_alias      = {}
        self.__map_workingdir = {}
        self.__repo_path      = repo_path
        
        return

    # create a new repo
    def register(self, workingdir):
        hashid = hashlib.sha1(workingdir).hexdigest()
        repo = Repo()
        repo.hashid = hashid
        repo.path   = os.path.join(self.__repo_path, hashid)

        self.__map_hashid[hashid]         = repo
        self.__map_workingdir[workingdir] = repo
        return repo

    # remove a hashid
    def unregister(self, hashid):
        try:
            repo = self.__map_hashid[hashid]
        except KeyError:
            print "hashid %s is not registered"%(hashid)
            return None
        
        for alias in repo.alias:
            try:
                del self.__map_alias[alias]
            except:
                continue

        for workingdir in repo.workingdir:
            try:
                del self.__map_workingdir[workingdir]
            except:
                continue
            
        del self.__map_hashid[hashid]
        return True


    # link an alias to a hashid
    def link_alias(self, alias, hashid):
        print alias, hashid
        if self.__map_alias.has_key(alias):
            print "alis %s has been used"%(alias)
            return None

        try:
            repo = self.__map_hashid[hashid]
        except KeyError:
            print "hashid %s is not registered"%(hashid)
            return None
        self.__map_alias[alias] = repo       
        repo.alias.append(alias)
        return True

    # unlink an alias
    def unlink_alias(self, alias):
        try:
            repo = self.__map_alias[alias]
        except KeyError:
            print "alis %s has not been set"%(alias)
            return None

        del self.__map_alias[alias]
        repo.alias.remove(alias)
        return True

        
    # given an alias, return the hashid
    def lookup_by_alias(self, alias):
        try:
            repo = self.__map_alias[alias]
        except KeyError:
            print "can not find repositry by alias %s "%(alias)
            return None
        return repo
        
    # given an working dir, return the hashid
    def lookup_by_workingdir(self, workingdir):
        try:
            repo = self.__map_workingdir[workingdir]
        except KeyError:
            print "working dir %s is not recognized"%(workingdir)
            return None
        return repo


    def lookup_by_hashid(self, hashid):
        try:
            repo = self.__map_hashid[hashid]
        except KeyError:
            print "can not find repositry by hashid %s "%(hashid)
            return None
        return repo


        
